﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DIS.Infrastructure.Entities.Entity;
using DIS.Infrastructure.Entities;

namespace DIS.Portal.Biz
{
    public class LongHaulBiz
    {
        public static ItemListBox<LongHaul> GetAllLongHaulFee(int page = 0, int pageSize = 20)
        {
            string username = System.Web.HttpContext.Current.User.Identity.Name;
            var count = new LongHaul().Count(c => c.UserName == username);
            using (Context ctx = new Context())
            {
                var data = (from c in ctx.LongHaul
                            join d in ctx.User
                            on c.UserName equals d.UserName
                            join f in ctx.Project
                           on c.ProjectId equals f.Id
                            where c.UserName == username
                            orderby c.Year, c.Month descending
                            select new { c, d,f })
                          .ToList()
                          .Select(e => new LongHaul().Set(e.c).LoadUser(e.d).LoadProject(e.f)).ToList();

                return new ItemListBox<LongHaul>(data).BuildPage(
                  count, page, 20, new PageParameter() { Style = "workspace" });
            }
        }

        public static LongHaul SaveLongHaulFee(LongHaul longHaul, List<LongHaulDetail> detailList)
        {
            if (longHaul.Id > 0)
            {
                var longHaulTemp = new LongHaul().Find(longHaul.Id.ToString());
                longHaulTemp.Year = longHaul.Year;
                longHaulTemp.Month = longHaul.Month;
                longHaulTemp.StartDate = longHaul.StartDate;
                longHaulTemp.EndDate = longHaul.EndDate;
                longHaulTemp.TotalDays = longHaul.TotalDays;
                longHaulTemp.TotalFee = longHaul.TotalFee;
                longHaulTemp.InvoiceCount = longHaul.InvoiceCount;
                longHaulTemp.ProjectId = longHaul.ProjectId;
                longHaulTemp.PostModify();
            }
            else
            {
                longHaul = longHaul.PostAdd();
            }
            foreach (var item in detailList)
            {
                item.LongHaulId = longHaul.Id;
                item.PostAdd();
            }
            return longHaul;
        }
    }
}
